using UnityEngine;
using System.Collections;

public class Music : MonoBehaviour {

	public AudioClip musiqueGame;
	public AudioClip musiqueMenu;
	private static int numinstance = 0;
	private int m_numInstance;

	public void Start()
	{
		m_numInstance = numinstance;
		numinstance++;
		DontDestroyOnLoad(this.gameObject);

		this.transform.position = GameObject.FindGameObjectWithTag("MainCamera").transform.position;
		if (Application.loadedLevelName == "Scenetest")
		{
			this.gameObject.GetComponent<AudioSource>().clip = musiqueGame;
			this.gameObject.GetComponent<AudioSource>().Play();
		}
		else
		{
			this.gameObject.GetComponent<AudioSource>().clip = musiqueMenu;
			this.gameObject.GetComponent<AudioSource>().Play();
		}
	}

	
	// Update is called once per frame
	void OnLevelWasLoaded () {
		//Debug.Log("level charge "+ Application.);
		if (m_numInstance == 0)
		{
			if (GameObject.FindGameObjectsWithTag("Musique").Length > 1)
			{
				for (int i = 0; i < GameObject.FindGameObjectsWithTag("Musique").Length-1; i++)
				{
					Destroy(GameObject.FindGameObjectsWithTag("Musique")[i].gameObject);
				}
			}

			this.transform.position = GameObject.FindGameObjectWithTag("MainCamera").transform.position;
			if (Application.loadedLevelName == "Scenetest" && this.gameObject.GetComponent<AudioSource>().clip != musiqueGame)
			{
				this.gameObject.GetComponent<AudioSource>().clip = musiqueGame;
				this.gameObject.GetComponent<AudioSource>().Play();
			}
			else if (this.gameObject.GetComponent<AudioSource>().clip != musiqueMenu && Application.loadedLevelName != "Scenetest")
			{
				this.gameObject.GetComponent<AudioSource>().clip = musiqueMenu;
				this.gameObject.GetComponent<AudioSource>().Play();
			}
		}
	}
	
}
